package com.maimao.core.utils;

import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author MaoLin Wang
 * @date 2020/12/14 10:49 下午
 */
public class IPUtil {

    /*public static String getIP(HttpServletRequest request) {
        //识别通过HTTP代理或负载均衡方式
        String ip = request.getHeader("x-forwarded-for");
        if (!checkIP(ip)) {
            //apache http做代理时一般会加上Proxy-Client-IP
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (!checkIP(ip)) {
            //weblogic插件加上的头
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (!checkIP(ip)) {
            ip = request.getRemoteAddr();
        }
        if(!checkIP(ip)){
            //从命令行获取本机IP
            ip = getLocalIPForCMD();
        }
        return ip;
    }
    private static boolean checkIP(String ip) {
        if (ip == null || ip.length() == 0 || "unkown".equalsIgnoreCase(ip)
                || ip.split(".").length != 4) {
            return false;
        }
        return true;
    }


    public static String getLocalIPForCMD(){
        StringBuilder sb = new StringBuilder();
        String command = "cmd.exe /c ipconfig | findstr IPv4";
        try {
            Process p = Runtime.getRuntime().exec(command);
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while((line = br.readLine()) != null){
                line = line.substring(line.lastIndexOf(":")+2,line.length());
                sb.append(line);
            }
            br.close();
            p.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }*/

    public static String getIpStr(HttpServletRequest request){
        String ipAddress = getIpAddress(request);
        String[] split = StringUtils.split(ipAddress, ":");
        StringBuilder sb=new StringBuilder();
        if (split.length>0){
            for (String s : split) {
                sb.append(s);
            }
            return sb.toString();
        }else if ((split=StringUtils.split(ipAddress,".")).length>0){
            for (String s : split) {
                sb.append(s);
            }
            return sb.toString();
        }
        return ipAddress;
    }
    public static String getIpAddress(HttpServletRequest request) {
        String Xip = request.getHeader("X-Real-IP");
        String XFor = request.getHeader("X-Forwarded-For");
        if (!Strings.isNullOrEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = XFor.indexOf(",");
            if (index != -1) {
                return XFor.substring(0, index);
            } else {
                return XFor;
            }
        }
        XFor = Xip;
        if (!Strings.isNullOrEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)) {
            return XFor;
        }
        if (Strings.nullToEmpty(XFor).trim().isEmpty() || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("Proxy-Client-IP");
        }
        if (Strings.nullToEmpty(XFor).trim().isEmpty() || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (Strings.nullToEmpty(XFor).trim().isEmpty() || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (Strings.nullToEmpty(XFor).trim().isEmpty() || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (Strings.nullToEmpty(XFor).trim().isEmpty() || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getRemoteAddr();
        }
        return XFor;
    }
}
